﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.RenameTool.RulesManager.CUiRulesSetManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.CodeDom.Compiler;
using System.Collections;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Markup;
using System.Windows.Media;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui.WPF;


namespace DnProcessSimulateCommands.RenameTool.RulesManager;

public class CUiRulesSetManager : TxWindow, IComponentConnector
{
  public ObservableCollection<RuleBase> Rules = new ObservableCollection<RuleBase>();
  private CApRulesSetManager rulesManager = CApRulesSetManager.Instance;
  private bool dialogWasCanceled;
  internal System.Windows.Controls.DataGrid dataGridSets;
  internal System.Windows.Controls.ListBox listRules;
  internal System.Windows.Controls.Button btnImport;
  internal System.Windows.Controls.Button btnExport;
  internal System.Windows.Controls.Button btnDelete;
  internal System.Windows.Controls.Button btnClose;
  internal System.Windows.Controls.Button btnOK;
  private bool _contentLoaded;

  public bool DialogWasCanceled
  {
    get => this.dialogWasCanceled;
    set => this.dialogWasCanceled = value;
  }

  public CUiRulesSetManager(Window parent)
  {
    ((Window) this).Owner = parent;
    this.InitializeComponent();
    // ISSUE: explicit non-virtual call
    __nonvirtual (((Window) this).Title) = XamlResourceManager.GetString("RENAME_TOOL_TITLE_RULESMANAGER");
  }

  public RuleMessage InitializeWindow()
  {
    RuleMessage ruleMessage = this.rulesManager.DeSerializeRenameRuleSet();
    this.dataGridSets.ItemsSource = (IEnumerable) this.rulesManager.RuleSets;
    if (this.rulesManager.RuleSets.Count > 0)
      this.dataGridSets.SelectedIndex = 0;
    return ruleMessage;
  }

  private void InitializeColumnsMinWidth()
  {
    if (this.rulesManager.RuleSets.Count == 0)
      return;
    string str1 = "";
    string str2 = "";
    foreach (CApRulesSet ruleSet in (Collection<CApRulesSet>) this.rulesManager.RuleSets)
    {
      if (str1.Length < ruleSet.Name.Length)
        str1 = ruleSet.Name;
      if (str2.Length < ruleSet.Description.Length)
        str2 = ruleSet.Description;
    }
    double num1 = this.MeasureWidth(str1);
    if (this.dataGridSets.Columns[0].MinWidth < num1)
      this.dataGridSets.Columns[0].MinWidth = num1;
    double num2 = this.MeasureWidth(str2);
    if (this.dataGridSets.Columns[1].MinWidth >= num2)
      return;
    this.dataGridSets.Columns[1].MinWidth = num2;
  }

  private double MeasureWidth(string str)
  {
    return new FormattedText(str, CultureInfo.CurrentUICulture, System.Windows.FlowDirection.LeftToRight, new Typeface(this.dataGridSets.FontFamily, this.dataGridSets.FontStyle, this.dataGridSets.FontWeight, this.dataGridSets.FontStretch), this.dataGridSets.FontSize, (Brush) Brushes.Black).Width + 10.0;
  }

  private void TxWindow_Loaded(object sender, RoutedEventArgs e)
  {
    this.InitializeColumnsMinWidth();
  }

  public static T GetVisualChild<T>(Visual parent) where T : Visual
  {
    visualChild = default (T);
    int childrenCount = VisualTreeHelper.GetChildrenCount((DependencyObject) parent);
    for (int childIndex = 0; childIndex < childrenCount; ++childIndex)
    {
      Visual child = (Visual) VisualTreeHelper.GetChild((DependencyObject) parent, childIndex);
      if (!(child is T visualChild))
        visualChild = CUiRulesSetManager.GetVisualChild<T>(child);
      else
        break;
    }
    return visualChild;
  }

  private void btnDelete_Click(object sender, RoutedEventArgs e)
  {
    for (int index = this.dataGridSets.SelectedItems.Count - 1; index >= 0; --index)
      this.rulesManager.RemoveSet(this.dataGridSets.Items.IndexOf(this.dataGridSets.SelectedItems[index]));
    this.dataGridSets.SelectedIndex = 0;
    if (this.rulesManager.RuleSets.Count != 0)
      return;
    this.dataGridSets.SelectedIndex = -1;
    this.listRules.ItemsSource = (IEnumerable) null;
  }

  private void btnExport_Click(object sender, RoutedEventArgs e)
  {
    RuleMessage ruleMessage = new RuleMessage(true);
    SaveFileDialog saveFileDialog = new SaveFileDialog();
    saveFileDialog.CheckPathExists = true;
    saveFileDialog.Filter = "xml files (*.xml)|*.xml";
    saveFileDialog.FilterIndex = 0;
    saveFileDialog.Title = "Export";
    if (saveFileDialog.ShowDialog() == DialogResult.Cancel)
      return;
    ObservableCollection<CApRulesSet> rules = new ObservableCollection<CApRulesSet>();
    foreach (CApRulesSet selectedItem in (IEnumerable) this.dataGridSets.SelectedItems)
      rules.Add(selectedItem);
    if (this.rulesManager.ExportRuleSet(saveFileDialog.FileName, rules).Success)
    {
      int num1 = (int) TxMessageBox.Show(XamlResourceManager.GetString("RENAME_TOOL_RULE_MANAGER_EXPORT_SUCCESS"), XamlResourceManager.GetString("RENAME_TOOL_RULE_MANAGER_SUCCESS"), MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
    }
    else
    {
      int num2 = (int) TxMessageBox.Show(XamlResourceManager.GetString("RENAME_TOOL_RULE_MANAGER_EXPORT_FAIL"), XamlResourceManager.GetString("RENAME_TOOL_ERROR"), MessageBoxButtons.OK, MessageBoxIcon.Hand);
    }
  }

  private void btnImport_Click(object sender, RoutedEventArgs e)
  {
    RuleMessage ruleMessage = new RuleMessage(true);
    using (OpenFileDialog openFileDialog = new OpenFileDialog())
    {
      openFileDialog.CheckFileExists = true;
      openFileDialog.CheckPathExists = true;
      openFileDialog.Multiselect = false;
      openFileDialog.Filter = "xml files (*.xml)|*.xml";
      openFileDialog.FilterIndex = 0;
      openFileDialog.Title = "Import";
      if (openFileDialog.ShowDialog() == DialogResult.Cancel)
        return;
      if (this.rulesManager.ImportRuleSet(openFileDialog.FileName).Success)
      {
        int num1 = (int) TxMessageBox.Show(XamlResourceManager.GetString("RENAME_TOOL_RULE_MANAGER_IMPORT_SUCCESS"), XamlResourceManager.GetString("RENAME_TOOL_RULE_MANAGER_SUCCESS"), MessageBoxButtons.OK, MessageBoxIcon.None);
      }
      else
      {
        int num2 = (int) TxMessageBox.Show(XamlResourceManager.GetString("RENAME_TOOL_RULE_MANAGER_IMPORT_FAIL"), XamlResourceManager.GetString("RENAME_TOOL_ERROR"), MessageBoxButtons.OK, MessageBoxIcon.Hand);
      }
    }
  }

  private void btnClose_Click(object sender, RoutedEventArgs e) => ((Window) this).Close();

  private void dataGridSets_SelectionChanged(object sender, SelectionChangedEventArgs e)
  {
    int selectedIndex = this.dataGridSets.SelectedIndex;
    if (selectedIndex == -1)
      return;
    this.listRules.ItemsSource = (IEnumerable) this.rulesManager.RuleSets[selectedIndex].Rules;
  }

  private void btnOK_Click(object sender, RoutedEventArgs e)
  {
    RuleMessage ruleMessage = this.rulesManager.SerializeRenameRuleSet();
    if (!ruleMessage.Success)
    {
      int num = (int) TxMessageBox.Show(ruleMessage.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
    }
    else
      ((Window) this).Close();
  }

  private void btnCancel_Click(object sender, RoutedEventArgs e)
  {
    this.rulesManager.DeSerializeRenameRuleSet();
    this.dialogWasCanceled = true;
  }

  private void dataGridSets_UnloadingRow(object sender, DataGridRowEventArgs e)
  {
    if (this.rulesManager.RuleSets.Count != 0)
      return;
    this.listRules.ItemsSource = (IEnumerable) null;
  }

  private void dataGridSets_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
  {
    if (!(e.EditingElement is System.Windows.Controls.TextBox) || e.EditAction != DataGridEditAction.Commit)
      return;
    System.Windows.Controls.TextBox editingElement = e.EditingElement as System.Windows.Controls.TextBox;
    if (editingElement.Text.Trim().Length == 0)
    {
      if (e.Column.DisplayIndex != 0)
        return;
      e.Cancel = true;
      int num = (int) TxMessageBox.Show(XamlResourceManager.GetString("RENAME_TOOL_RULE_MANAGER_EMPTY_NAME_ERRORMSG"), XamlResourceManager.GetString("ERROR"), MessageBoxButtons.OK, MessageBoxIcon.Hand);
      this.dataGridSets.CancelEdit(DataGridEditingUnit.Cell);
    }
    else
    {
      double num = this.MeasureWidth(editingElement.Text);
      if (e.Column.MinWidth >= num)
        return;
      e.Column.MinWidth = num;
    }
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  public void InitializeComponent()
  {
    if (this._contentLoaded)
      return;
    this._contentLoaded = true;
    System.Windows.Application.LoadComponent((object) this, new Uri("/DnProcessSimulateCommands;component/renametool/rulesmanager/cuirulessetmanager.xaml", UriKind.Relative));
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  [EditorBrowsable(EditorBrowsableState.Never)]
  void IComponentConnector.Connect(int connectionId, object target)
  {
    switch (connectionId)
    {
      case 1:
        this.dataGridSets = (System.Windows.Controls.DataGrid) target;
        this.dataGridSets.SelectionChanged += new SelectionChangedEventHandler(this.dataGridSets_SelectionChanged);
        this.dataGridSets.UnloadingRow += new EventHandler<DataGridRowEventArgs>(this.dataGridSets_UnloadingRow);
        this.dataGridSets.CellEditEnding += new EventHandler<DataGridCellEditEndingEventArgs>(this.dataGridSets_CellEditEnding);
        break;
      case 2:
        this.listRules = (System.Windows.Controls.ListBox) target;
        break;
      case 3:
        this.btnImport = (System.Windows.Controls.Button) target;
        this.btnImport.Click += new RoutedEventHandler(this.btnImport_Click);
        break;
      case 4:
        this.btnExport = (System.Windows.Controls.Button) target;
        this.btnExport.Click += new RoutedEventHandler(this.btnExport_Click);
        break;
      case 5:
        this.btnDelete = (System.Windows.Controls.Button) target;
        this.btnDelete.Click += new RoutedEventHandler(this.btnDelete_Click);
        break;
      case 6:
        this.btnClose = (System.Windows.Controls.Button) target;
        this.btnClose.Click += new RoutedEventHandler(this.btnCancel_Click);
        break;
      case 7:
        this.btnOK = (System.Windows.Controls.Button) target;
        this.btnOK.Click += new RoutedEventHandler(this.btnOK_Click);
        break;
      default:
        this._contentLoaded = true;
        break;
    }
  }
}
